/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomersList.java
 *
 * Created on 10/06/2011, 01:55:01
 */

package com.classicmodels.view;

import com.classicmodels.model.Customer;
import java.lang.reflect.Field;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mauricio
 */
public class CustomersList extends javax.swing.JFrame {

  private DefaultTableModel modelo=new DefaultTableModel();
    private Customer[] c;
    /** Creates new form CustomersList */
    public CustomersList(Customer[] customers) {
        c = customers;
        SetModelo(customers);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        deleteButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        editCustomer = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 18));
        jLabel1.setText("Classic Models Inc - Customers");

        deleteButton.setText("Delete");

        newButton.setText("New Customer");
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });

        editCustomer.setText("Edit Customer");
        editCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCustomerActionPerformed(evt);
            }
        });

        jTable1.setModel(modelo);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(317, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(deleteButton)
                                .addGap(78, 78, 78)
                                .addComponent(editCustomer)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 226, Short.MAX_VALUE)
                                .addComponent(newButton))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(22, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(deleteButton)
                    .addComponent(editCustomer))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void editCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCustomerActionPerformed
        // TODO add your handling code here:
               
        
    }//GEN-LAST:event_editCustomerActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
            // TODO add your handling code here:
        CustomerForm customerForm = new CustomerForm();
        customerForm.setVisible(true);
        initComponents();
    }//GEN-LAST:event_newButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new CustomersList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editCustomer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton newButton;
    // End of variables declaration//GEN-END:variables

public void SetModelo(Customer[] customers){
   
    /*
    modelo.addColumn("customerNumber");
    modelo.addColumn("customerName");
    modelo.addColumn("contactFirstName");
    modelo.addColumn("contactLastName");
    modelo.addColumn("phone");
    modelo.addColumn("addressLine1");
    modelo.addColumn("addressLine2");
    modelo.addColumn("city");
    modelo.addColumn("state");
    modelo.addColumn("postalCode");
    modelo.addColumn("country");
    modelo.addColumn("salesRepEmployeeNumber");
    modelo.addColumn("creditLimit");
  
    //Se ppuede hacer asi como arriba o sino....
    REFLECTION
     */
    Field[] campos= Customer.class.getDeclaredFields();
    String[] nombreDeCampos=new String [campos.length];
    int i=0;
    
    for (Field f:campos){
        modelo.addColumn(f.getName());
        
        //nombreDeCampos[i++]=f.getName();
    }
           
    for (Customer customer:customers){
        modelo.addRow(customer.Cargar_Datos());
    } 
    
}    

   
    
}
